package com.sdy.utils;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.ImmutableMap;
import com.sdy.po.Notic;
import com.sdy.po.Parm;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;

@Component
public class NoticUtil {

    /**
     * 将获得的信息转换成一则推送
     * @param beforeState
     * @param companyId
     * @param parm
     * @param jsonObject
     * @return
     */
    public Notic dataToNotic(String beforeState,Integer companyId, Parm parm, JSONObject jsonObject){

        String nowState = jsonObject.get("dataState").toString();
        System.out.println(nowState);

        Notic notic = new Notic();

        notic.setCompanyId(companyId);
        notic.setTitle(getTitle(nowState,parm.getName()));
        notic.setParmId(parm.getId());
        notic.setData(((JSONObject)jsonObject.get("sequenceQueue")).toJSONString());
        notic.setInfo(getInfo(parm.getName(),beforeState,nowState));
        notic.setNoticState(nowState);
        notic.setReadState(Contact.NOTIC_READSTATE_UNREAD);
        notic.setTime(new Date());

        return notic;
    }

    /**
     * 拼接报告
     * @param parmName
     * @param beforeState
     * @param nowState
     * @return
     */
    public String getInfo(String parmName, String beforeState, String nowState){
        String info = Contact.NOTIC_INFO_ABNOEMAL_PARM + parmName + Contact.NOTIC_INFO_STATE_BEDORE + Contact.STATE_TO_CN.get(beforeState);
        if (Contact.STATE_TO_INT.get(beforeState) > Contact.STATE_TO_INT.get(nowState)){
            info += Contact.NOTIC_INFO_STATE_DOWN;
        }
        else {
            info += Contact.NOTIC_INFO_STATE_UP;
        }

        info += Contact.STATE_TO_CN.get(nowState) + Contact.NOTIC_INFO_FOOTER;

        return info;
    }

    /**
     * 拼接标题
     * @param state
     * @param parmName
     * @return
     */
    public String getTitle(String state,String parmName){
        return Contact.NOTIC_TITLE_HEAD1 + parmName + Contact.NOTIC_TITLE_HEAD2 + Contact.STATE_TO_CN.get(state);
    }

}
